package com.aditya.testmvcapp.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.aditya.testmvcapp.domain.PriceIncrease;
import com.aditya.testmvcapp.domain.Product;
import com.aditya.testmvcapp.persistence.ApplicationDao;
import com.aditya.testmvcapp.service.PriceIncreaseService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	private ApplicationDao appDao;
	
	@Autowired
	private PriceIncreaseService service;
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		List<Product> products = appDao.getProducts();
		
		model.addAttribute("serverTime", formattedDate );
		
		model.addAttribute("products", products);
		
		return "home";
	}
	
	@RequestMapping(value = "/addNewProduct.htm", method = RequestMethod.GET)
	public String addNewProduct(Model model){
		model.addAttribute(new Product());
		return "addproduct";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String addProduct(@Valid Product product,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "addproduct";
		}
		
		appDao.addProduct(product);
		return "redirect:/";
	}
	
	@RequestMapping(value = "/PriceIncrease.htm", method = RequestMethod.GET)
	public String loadPriceIncreasePage(Model model){
		model.addAttribute(new PriceIncrease());
		model.addAttribute("products",appDao.getProducts());
		return "increasePrice";
	}
	
	@RequestMapping(value = "/increasePrice.htm", method = RequestMethod.POST)
	public String increasePrice(@Valid PriceIncrease priceIncrease,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "increasePrice";
		}
		
		logger.info("Product:"+priceIncrease.getDescription());
		service.increasePrice(priceIncrease);
		return "redirect:/";
	}
	
	public ApplicationDao getAppDao() {
		return appDao;
	}
	
	public void setAppDao(ApplicationDao appDao) {
		this.appDao = appDao;
	}

	public PriceIncreaseService getService() {
		return service;
	}

	public void setService(PriceIncreaseService service) {
		this.service = service;
	}
	
}
